package admin

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"jwgin/model/mysql"
	"jwgin/service"
)

type ActivityTemplate struct {
}

func (l *ActivityTemplate) Routers(Router *gin.RouterGroup) (R gin.IRoutes) {
	Router.GET("atemplate", l.get)
	Router.PUT("atemplate", l.put)
	Router.POST("atemplate", l.post)
	return Router
}

//获取所有模板
func (ActivityTemplate) get(c *gin.Context) {
	count, size, offset := pages(c)
	db := service.G.Model(mysql.ActivityTemplate{})

	//名称
	if template_name := c.Query("title"); template_name != "" {
		db = db.Where("template_name = ?", template_name)
	}

	//状态
	if statu := c.Query("statu"); statu != "" {
		db = db.Where("template_statu = ?", statu)
	}

	list := []map[string]interface{}{}
	db.Count(&count).Offset(offset).Limit(size).Order("id DESC").Scan(&list)
	RespList(c, count, list)
}

//添加模板
func (ActivityTemplate) put(c *gin.Context) {
	requ := &mysql.ActivityTemplate{}
	if err := c.ShouldBind(requ); err != nil {
		RespErr(c, err)
		return
	}

	if len(requ.TemplateName) == 0 || len(requ.TemplateIden) == 0 || len(requ.TemplateInfo) == 0 || len(requ.TemplateWebPath) == 0 {
		RespErr(c, fmt.Errorf("参数有问题"))
		return
	}

	if requ.TemplateStatu < 1 {
		requ.TemplateStatu = 2
	}
	service.G.Create(requ)
	RespSucc(c, nil)
}

//更新模板
func (ActivityTemplate) post(c *gin.Context) {
	requ := &mysql.ActivityTemplate{}
	if err := c.ShouldBind(requ); err != nil {
		RespErr(c, err)
		return
	}
	if requ.Id == 0 {
		RespErr(c, fmt.Errorf("参数有问题"))
		return
	}
	service.G.Where("id = ? ", requ.Id).Updates(requ)
	RespSucc(c, nil)
}
